<?php
require_once("includes/connect.php");
require_once("includes/function.php");

$card_id = $_REQUEST["card_id"];
$panngan2_id = $_REQUEST["panngan2_id"];
$panngan3_id = $_REQUEST["panngan3_id"];

$db = NEW myDBS();

$sql = "SELECT * "
        . " FROM tbl_payment "
        . " WHERE card_id = '$card_id' ";
$row = $db->fetch_object($db->runSQL($sql));

//echo $row->branch_id;
?>

<script type="text/javascript">
    $(document).ready(function() {


        $("#optionsRadios1").click(function() {
            $('#account_name').attr("disabled", $(this).is(":checked"));
            $('#account_no').attr("disabled", $(this).is(":checked"));
            $('#bank_id').attr("disabled", $(this).is(":checked"));
            $('#branch_id').attr("disabled", $(this).is(":checked"));
        });

        $('#optionsRadios1').trigger('click');

        $("#optionsRadios2").click(function() {
            //alert($(this).is(":checked"));
            $('#account_name').attr("disabled", $(this).is(":checked"));
            $('#account_no').attr("disabled", $(this).is(":checked"));
            $('#bank_id').attr("disabled", $(this).is(":checked"));
            $('#branch_id').attr("disabled", $(this).is(":checked"));
        });

        $("#optionsRadios3").click(function() {
            //alert("adfads");
            $('#account_name').attr("disabled", false);
            $('#account_no').attr("disabled", false);
            $('#bank_id').attr("disabled", false);
            $('#branch_id').attr("disabled", false);
        });

        $("input,select,textarea").not("[type=submit]").jqBootstrapValidation({
            submitSuccess: function($form, event) {
                event.preventDefault();
                var panngan2_id = "<?php echo $panngan2_id; ?>";
                var panngan3_id = "<?php echo $panngan3_id; ?>";

                $.ajax({
                    type: "POST",
                    url: "payment_process.php",
                    data: $('#frm_main').serialize(),
                    cach: false,
                    error: function() {
                        alert("ERROR !!!!");
                    },
                    success: function(data) {
                        //alert(data);
                        window.location.href = "index.php?pages=person_payment&panngan2_id=" + panngan2_id + "&panngan3_id=" + panngan3_id;

                    }
                });
            }
        });
    });
</script>
<form method="post" id="frm_main" name="frm_main" novalidate>
    <label class="radio">
        <input type="radio" name="payment_method" id="optionsRadios1" value="cash" <?php echo ($row->payment_method == "cash") ? "checked" : ""; ?>>
        เงินสด
    </label>
    <label class="radio">
        <input type="radio" name="payment_method" id="optionsRadios2" value="cheque" <?php echo ($row->payment_method == "cheque") ? "checked" : ""; ?>>
        เช็ค
    </label>
    <label class="radio">
        <input type="radio" name="payment_method" id="optionsRadios3" value="trans" <?php echo ($row->payment_method == "trans") ? "checked" : ""; ?>>
        เงินโอน
    </label>
    <div class="control-group">
        <label>ชื่อบัญชี</label>
        <div class="controls">
            <input type="text" id="account_name" name="account_name" value="<?php echo $row->account_name; ?>">
        </div>
    </div>
    <div class="control-group">
        <label>เลขที่บัญชี</label>
        <div class="controls">
            <input type="text" id="account_no" name="account_no" value="<?php echo $row->account_no; ?>">
        </div>
    </div>

    <div class="control-group">
        <label>ธนาคาร</label>
        <div class="controls">
            <select class="span5" id="bank_id" name="bank_id" >
                <option value="">เลือก...</option>
                <?php
                $result = $db->showData("bank");
                while ($rows = $db->fetch_object($result)) {
                    if ($rows->bank_id == $row->bank_id) {
                        $selected = "selected";
                    } else {
                        $selected = "";
                    }

                    echo "<option value='$rows->bank_id' $selected>$rows->bank_name</option>";
                }
                ?>

            </select>
        </div>
    </div>
    <div class="control-group">
        <label>สาขา</label>
        <div class="controls">

            <select class="span4" id="branch_id" name="branch_id" >
                <option value="">เลือก...</option>
                <?php
                $result = $db->showData("branch");

                while ($rows = $db->fetch_object($result)) {
                    if ($rows->branch_id == $row->branch_id) {
                        $selected = "selected";
                    } else {
                        $selected = "";
                    }
                    echo "<option value='$rows->branch_id' $selected>$rows->branch_name</option>";
                }
                ?>
            </select>
        </div>
    </div>

    <div class="control-group">
        <label>ประเภทบัญชี</label>
        <div class="controls">
            <select class="span5" id="bank_type" name="bank_type" >
                <option value="">เลือก...</option>
                <?php
                $sql = "SELECT DISTINCT bank_type "
                        . " FROM bank_account "
                        . " ";
                $result = $db->runSQL($sql);

                while ($rows = $db->fetch_object($result)) {
                    echo "<option>$rows->bank_type</option>";
                }
                ?>

            </select>
        </div>
    </div>

    <div class="form-actions">
        <input type="hidden" id="id" name="card_id" value="<?php echo $card_id; ?>">
        <input type="hidden" id="task" name="task" value="<?php echo ($row->id) ? "edit" : "new"; ?>">

        <button type="submit" class="btn btn-info" id="save_changes" >บันทึกข้อมูล</button>
        <!--<input type="submit" class="btn btn-info" value="บันทึกข้อมูล">-->
        <a class="btn close-dialog"><i class="icon-remove"></i> ยกเลิก</a>

    </div>
</form>